package com.lphl.pet.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnSwipeListener;
import com.lphl.pet.GloableParams;
import com.lphl.pet.R;
import com.lphl.pet.adapter.PicListAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PicListActivity extends Activity implements OnClickListener {

    private PicListAdapter mAdapter;
    private SwipeMenuListView mListView;
    private ImageView iv_thumb;
    private ImageView iv_back;
    private ImageView iv_down_icon;
    public String[] allFiles;
    private List<String> picPathList;

    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            if(mAdapter == null){
                mAdapter = new PicListAdapter(PicListActivity.this, picPathList);
            }
            mListView.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
        };
    };

    @Override
    protected void onResume() {
        super.onResume();
        // 是否删除图片
        int p = getIntent().getIntExtra("isRemove", 1);
        if(p==GloableParams.isRemove){
            picPathList.remove(GloableParams.mPosition);
            mAdapter.notifyDataSetChanged();
        }

        // 重命名后, 刷新显示图片名称
        boolean isRename = getIntent().getBooleanExtra("isRename", false);
        if(isRename){
            mAdapter.notifyDataSetChanged();
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_list);

        initView();
        initData();
    }

    private void initData() {
        File folder = new File(Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/");
        allFiles = folder.list();
        // 转为集合
        picPathList = new ArrayList(Arrays.asList(allFiles));

        handler.sendEmptyMessage(0);
    }

    private void initView() {
        iv_thumb = (ImageView) findViewById(R.id.iv_thumb);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_down_icon = (ImageView) findViewById(R.id.iv_down_icon);
        mListView = (SwipeMenuListView) findViewById(R.id.listView);

        iv_back.setOnClickListener(this);
        iv_down_icon.setOnClickListener(this);
        iv_thumb.setOnClickListener(this);


        // step 1. create a MenuCreator
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                // create "open" item
                SwipeMenuItem openItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                        0xCE)));
                // set item width
                openItem.setWidth(dp2px(90));
                // set item title
                openItem.setTitle("分享");
                // set item title fontsize
                openItem.setTitleSize(18);
                // set item title font color
                openItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(openItem);

                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set a icon
                deleteItem.setIcon(R.drawable.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        // set creator
        mListView.setMenuCreator(creator);

        // step 2. listener item click event
        mListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
//                ApplicationInfo item = mAppList.get(position);
                switch (index) {
                    case 0:
                        // open
                        break;
                    case 1:
                        // delete
                        delete(position);
                        picPathList.remove(position);
                        mAdapter.notifyDataSetChanged();
                        break;
                }
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(PicListActivity.this, ShowPicActivity.class);
                intent.putExtra("imgUrl",picPathList.get(position));
                GloableParams.mPosition = position;
                startActivity(intent);
            }
        });

        // set SwipeListener
        mListView.setOnSwipeListener(new OnSwipeListener() {

            @Override
            public void onSwipeStart(int position) {
                // swipe start
//                Toast.makeText(PicListActivity.this,"onSwipeStart",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onSwipeEnd(int position) {
                // swipe end
//                Toast.makeText(PicListActivity.this,"onSwipeEnd",Toast.LENGTH_LONG).show();
            }
        });

        // other setting
//		listView.setCloseInterpolator(new BounceInterpolator());

    }

      private void delete(int position) {

        String path = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/"+picPathList.get(position);
        File delFile = new File(path);
          if(delFile.exists()){
              delFile.delete();
              Toast.makeText(getApplicationContext(),"删除成功",Toast.LENGTH_LONG).show();
          }else{
              Toast.makeText(getApplicationContext(),"文件不存在",Toast.LENGTH_LONG).show();
          }
    }

    /**
     * 打开
     * @param position
     */
    private void open(int position) {

    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                this.finish();
                break;

            case R.id.iv_down_icon:
                Toast.makeText(getApplicationContext(), "点击了下拉菜单", Toast.LENGTH_LONG).show();
                break;

            case R.id.iv_thumb:
                Toast.makeText(getApplicationContext(), "点击了显示缩略图菜单", Toast.LENGTH_LONG).show();
                break;

            default:
                break;
        }
    }
}
